package com.blue.tooth.lock.util;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.meihua.passivelock.fastble.data.BleDevice;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * author: jiangliqiang
 * date:   2020/09/22
 */
public class AppCache {
    private static BleDevice bleDevice;

    public static BleDevice getBleDevice() {
        return bleDevice;
    }

    public static void setBleDevice(BleDevice bleDevice) {
        AppCache.bleDevice = bleDevice;
    }


    public static boolean isInstallApp(Context context, String uri) {
        PackageManager pm = context.getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }

    public static boolean isInstallApp(String packageName, Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        List<String> pName = new ArrayList<String>();
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                pName.add(pn);
            }
        }
        return pName.contains(packageName);
    }
}
